package com.controller;

import com.bean.Customer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
public class JsonController {
    @RequestMapping("/testJson")
    public String JsonMethod() {
        Customer customer = new Customer();
        customer.setId(1111);
        customer.setPassword("123456");
        customer.setUsername("admin");
        customer.setAge(19);
        customer.setEmail("111@163.com");

        Customer cu = new Customer();
        cu.setId(222);
        cu.setPassword("2222");
        cu.setUsername("admin2");
        cu.setAge(20);
        cu.setEmail("111@126.com");

        List<Customer> listCu = new ArrayList<>();
        listCu.add(customer);
        listCu.add(cu);
        ObjectMapper obj = new ObjectMapper();
        try {
            //返回一个对象json
//            return obj.writeValueAsString(customer);
            return obj.writeValueAsString(listCu);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;

    }
}
